package com.titaniume.activitiweb.security;


import com.titaniume.activitiweb.mapper.UserInfoBeanMapper;
import com.titaniume.activitiweb.pojo.UserInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class MyUserDetailisService  implements UserDetailsService {


    @Autowired
    UserInfoBeanMapper userInfoBeanMapper;

    @Override
    public UserDetails loadUserByUsername(String username)  throws UsernameNotFoundException {
//        String password = passwordEncoder().encode("111");
//        return  new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ACTIVITI_USER"));

        //读取数据库判断用户
        //如果用户是null抛出异常
        //返回用户信息

        UserInfoBean userInfoBean = userInfoBeanMapper.selectByUserName(username);
        if (userInfoBean == null) {
            throw new UsernameNotFoundException("数据库中无此用户");
        }


        return userInfoBean;
    }


    //加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
